package com.kly.user.task.v2;

import cn.hutool.core.net.NetUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.kly.user.db.dao.UserPreHomepageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class RemoveHomepageCacheTask {
    private static Log logger = LogFactory.getLog(RemoveHomepageCacheTask.class);

    @NacosValue(value = "${clear.homepage.cache.mins:10}", autoRefreshed = true)
    private int homepageExpiredMins;


    @NacosValue(value = "${clear.expired.homepage.switch:false}", autoRefreshed = true)
    private boolean clearExpiredHomepageSwitch;

    @Autowired
    private UserPreHomepageDao userPreHomepageDao;

    @NacosValue(value = "${task.job.executor.ip}", autoRefreshed = true)
    private String taskJobExecutor;

    private String localIP = NetUtil.getLocalhostStr();

    @Scheduled(fixedDelay = 1000 * 60)
    public void run() {

        if (!taskJobExecutor.contains(localIP)) {
            logger.info("=============NotTaskJobExecutor, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, taskJobExecutor);
            return;
        }

        logger.info("== RemoveHomepageCacheTask start ==");

        if (!clearExpiredHomepageSwitch) {
            logger.info("== RemoveHomepageCacheTask start == switch not open");
        }

        userPreHomepageDao.removeRemoveUnEffectiveRecord();


    }

}
